home *** CD-ROM | disk | FTP | other *** search
- Path: ornews.intel.com!news
- From: thurman_b_miller@ccm2.hf.intel.com (Thurman Miller)
- Newsgroups: comp.lang.c++
- Subject: Re: Syntax for looping through enumerated types
- Date: Tue, 09 Jan 1996 17:59:02 GMT
- Organization: Intel Corporation
- Message-ID: <4cuafk$rgj@ornews.intel.com>
- References: <4cs70o$8ns@ornews.intel.com> <4ctiuo$qsi@klein.delphi.com>
- NNTP-Posting-Host: thurman-pc.ssd.intel.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Martin Miller <mrmiller@mci.newscorp.com> wrote:
-
-
- >Here's some things you may be able to do in specific cases, _although it doesn't
- >work for enums in general_ (and pardon me if you already know most of this):
-
- >You can take advantage of the fact that enums may be cast implicitly to int and and
- >they can be explicitly cast to enum types. This along with the important point that
- >named enum values start off at zero by default and, unless overridden, increase by
- >one with each successive name listed.
-
- >In your example it means that MONDAY cast to an integer would yield 0 and that
- >FRIDAY would give a value of 5. You can also specify names that repeat integer
- >values already used. Your example could be modified as follows:
-
- >enum daysoftheweek {MONDAY, FIRSTDAY=MONDAY/* added */, TUESDAY, WEDNESDAY,
- > THURSDAY, FRIDAY, LASTDAY=FRIDAY/* added */};
-
- >for (int i=FIRSTDAY; i<=LASTDAY; i++)
- >{
- > daysoftheweek nIndex = daysoftheweek(i);
- > switch (nIndex)
- > {
- > case MONDAY:
- > doMonday();
- > }
- >}
-
- Awesome! Just what the doctor ordered!
-
- >Once again, let me reiterate that this won't work in general, because enum integer
- >values can start at any where, jump around, and even repeat--but it might work for
- >you if you can just setup and follow a few conventions in your own code.
-
- Yes, but as long as we keep the def's for FIRSTDAY and LASTDAY in the
- eumerated list, it will work!
-
- >As I mentioned in another post to this group--in the past in order to handle more
- >general cases and reuse code, I created an extensible "class Enum" along with some
- >preprocessor macros. In the Enum class, among other things, I had member functions
- >which returned the starting and ending values.
-
- Didn't you mention in the other post that you no longer had the source
- :(, because if you did, I'd love to see it!
-
- >In addition, there were number of constructors and other operators which facilated
- >not only in using them in for-statements, but also in being able to convert them to
- >and from strings (ie associating MONDAY with the string "MONDAY") and
- >incrementing/decrementing their values.
-
- >In the other post I also mentioned that I wished the language itself supported some
- >of these things more automatically since they seem to be a very common things to
- >need and want to do.
-
- Ditto
-
- >If you (or anyone else reading this) have any further questions or comments about
- >this subject, feel free to contact me via my email address.
-
- >Hope this gives you some ideas.
-
- >--
- >Martin----/\/\./\/\.
-
-
-
-
-